#include <iostream>
using std::cout;
using std::endl;

class Singleton{
public:
    static Singleton * getInstance(){
        pthread_once(&_once_control, init);
        return _ps;
    }

    void print(){
        cout << "Singleton::print()" << endl;
    }

private:
    static void init(){
        _ps = new Singleton();
    }
    class AutoRelease{
    public:
        ~AutoRelease(){
            if(_ps){
                delete _ps;
                _ps = nullptr;
            }
        }
    };
    static Singleton * _ps;
    static AutoRelease _ar;
    static pthread_once_t _once_control;
};
Singleton * Singleton::_ps = nullptr;
Singleton::AutoRelease Singleton::_ar;
pthread_once_t Singleton::_once_control = PTHREAD_ONCE_INIT;

void test(){
    Singleton::getInstance()->print();
}

int main()
{
    test();
    return 0;
}

